﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using VNet.VisiSuite.Common.Resource;
using VNet.VisiSuite.VisiAdmin.Common.Helpers;

namespace VNet.VisiSuite.VisiAdmin.Models
{
    public class PlantModel : ComparableBaseModel<PlantModel>, IDataErrorInfo
    {
        public PlantModel()
        {
            Areas = new ObservableCollection<AreaModel>();
        }
        private int _plantId;
        
        public int PlantId
        {
            get { return _plantId; }
            set
            {
                _plantId = value;
                OnPropertyChanged("PlantId");
            }
        }

        public bool IsSaveEnabled
        {
            get
            {
                return IsChange && !string.IsNullOrEmpty(_plantName) && !string.IsNullOrEmpty(_description);
            }
        }

        private string _plantName;

        [Category("Properties")]
        [DisplayName("Name")]
        [Description("Plant name.")]
        [DefaultValue("")]
        public string PlantName
        {
            get { return _plantName; }
            set
            {
                _plantName = value;
                OnPropertyChanged("PlantName", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        private string _description;

        [Category("Properties")]
        [DisplayName("Description")]
        [Description("Plant description.")]
        [DefaultValue("")]
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged("Description", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        private string _date_format;
        [Category("Properties")]
        [DisplayName("Date Format")]
        [Description("Format for showing dates in the applications.")]
        [DefaultValue("d-MMM-yyyy h:mm tt")]
        public string DateFormat
        {
            get { return _date_format; }
            set
            {
                _date_format = value;
                OnPropertyChanged("DateFormat", true);
            }
        }

        private bool _enabled;
        [Category("Properties")]
        [DisplayName("Enabled")]
        [Description("Enable / disabled this plant.")]
        [DefaultValue("true")]
        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                _enabled = value;
                OnPropertyChanged("Enabled", true);
            }
        }

        private string _createdBy;
        [Category("Update History")]
        [DisplayName("Created By")]
        [Description("User who created the plant in VisiAdmin.")]
        public string CreatedBy
        {
            get { return _createdBy; }
            set
            {
                _createdBy = value;
                OnPropertyChanged("CreatedBy");
            }
        }

        private string _createdDate;
        [Category("Update History")]
        [DisplayName("Created On")]
        [Description("Date when the plant was created in VisiAdmin.")]
        public string CreatedDate
        {
            get { return _createdDate; }
            set
            {
                _createdDate = value;
                OnPropertyChanged("CreatedDate");
            }
        }

        private string _lastUpdatedBy;
        [Category("Update History")]
        [DisplayName("Updated By")]
        [Description("User who last edited the plant in VisiAdmin.")]
        public string LastUpdatedBy
        {
            get { return _lastUpdatedBy; }
            set
            {
                _lastUpdatedBy = value;
                OnPropertyChanged("LastUpdatedBy");
            }
        }

        

        private string _lastUpdatedDate;
        [Category("Update History")]
        [DisplayName("Updated On")]
        [Description("Date when the plant was last edited in VisiAdmin.")]
        public string LastUpdatedDate
        {
            get { return _lastUpdatedDate; }
            set
            {
                _lastUpdatedDate = value;
                OnPropertyChanged("LastUpdatedDate");
            }
        }

        private ObservableCollection<AreaModel> _areas;

        public ObservableCollection<AreaModel> Areas
        {
            get { return _areas; }
            set
            {
                if (_areas == value) return;
                _areas = value;
                OnPropertyChanged("Areas");
            }
        }

        #region IDataErrorInfo Implementation

        public string Error
        {
            get { return string.Empty; }
        }

        public string this[string columnName]
        {
            get
            {
                if (!ShouldValidate)
                    return string.Empty;

                if (columnName == "PlantName" && string.IsNullOrWhiteSpace(PlantName))
                    return ValidationMessages.PlantNameRequired;
                if (columnName == "Description" && string.IsNullOrWhiteSpace(Description))
                    return ValidationMessages.DescriptionRequired;
                if (columnName == "DateFormat" && string.IsNullOrWhiteSpace(DateFormat))
                    return ValidationMessages.DateFormatRequired;
                return string.Empty; ;
            }
        }

        #endregion

        #region Override Equals methods
        public bool Equals(PlantModel other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;

            return string.Equals(PlantName, other.PlantName)
                && string.Equals(Description, other.Description)
                && Enabled == other.Enabled
                && string.Equals(DateFormat, other.DateFormat)
                && Areas.IsCollectionEqual(other.Areas);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != this.GetType())
                return false;
            return Equals((PlantModel)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var result = 0;
                result = (result * 397) ^ (PlantName ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (Description ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (DateFormat ?? string.Empty).GetHashCode();
                result = (result * 397) ^ Enabled.GetHashCode();
                result = (result * 397) ^ (Areas ?? new ObservableCollection<AreaModel>()).GetHashCode();
                return result;
            }
        }
        #endregion
    }
}
